#include "userlogger.h"
#include "ettadatastruct.h"
#include "commonfunction.h"
#include "globaldef.h"
#include "sqlhelper.h"
#include "authmanager.h"
#include <QDebug>

static void record(const LogType &logtype, const QString &detail, const QString &currentUsername)
{
    LogInfo logInfo;
    logInfo.datetime = currentDateTime();
    logInfo.username = currentUsername;
    logInfo.type = logtype;
    logInfo.detail = detail;
    SqlHelper::getInstance()->insertLogInfo(logInfo);
}

static QString currentUser(const QString &username)
{
    return (username.trimmed().length() > 0) ? username.trimmed() : "-";
}

void UserLogger::log(const QString &detail)
{
    record(OPERATION, detail, currentUser(currentUsername));
}

void UserLogger::error(const QString &detail)
{
    record(ERROR_LOG, detail, currentUser(currentUsername));
}

void UserLogger::debug(const QString &detail)
{
    qDebug() << detail;
}
